package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.Adapter.OrderAdapter;
import com.huawei.hmsdemo10086.POJO.Order;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OrderActivity extends AppCompatActivity {

    private ListView listView;
    private List<Order> datas;
    private OrderAdapter orderAdapter;
    private TextView mTitleTextView;
    private Button back;
    private SharedPreferences sharedPreferences;
    private String userId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);

        listView = (ListView)this.findViewById(R.id.order_list);


        datas = new ArrayList<Order>();
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mTitleTextView.setText("订单管理");
        back = (Button)findViewById(R.id.button_backward);
        getOrders();


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(OrderActivity.this, Order_DetailActivity.class);
                intent.putExtra("goods_name", datas.get(position).getGoods().getName());
                intent.putExtra("goods_price", ""+datas.get(position).getGoods().getPrice() * datas.get(position).getNum());
                intent.putExtra("goods_src", datas.get(position).getGoods().getPicaddress());
                intent.putExtra("goods_type", ""+datas.get(position).getGoods().getType());
                intent.putExtra("goods_num", ""+datas.get(position).getNum());
                intent.putExtra("address_id", ""+datas.get(position).getAddressid());
                intent.putExtra("order_id", ""+datas.get(position).getOrderid());
                intent.putExtra("goods_id", ""+datas.get(position).getGoodsid());
                intent.putExtra("goods_img_url", ""+datas.get(position).getGoods().getPicaddress());
//                NewsData tmp = datas.get(position);
//                datas.remove(position);
//                datas.add(0,tmp);
//                adapter.notifyDataSetChanged();

                startActivity(intent);//启动Activity
            }
        });
    }

    private void getOrders()
    {
        String url = "http://10.0.2.2:8443/api/findorder";
        OkHttpClient mOkHttpClient = new OkHttpClient();
        String json = "{\n" +
                "  \"userid\": \"" + userId + "\"\n" +
                "}";
        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                Type type = new TypeToken<Result<List<Order>>>(){}.getType();
                Result<List<Order>> result = null;
                result = gson.fromJson(rep,type);
                for(int i = 0 ; i < result.getData().size() ; i++)
                {
                    System.out.println(result.getData().get(i).toString());
                }
                datas = result.getData();
                orderAdapter = new OrderAdapter(OrderActivity.this,datas);
                listView.setAdapter(orderAdapter);
                orderAdapter.notifyDataSetChanged();


            }
        });
    }
}